package cn.edu.scau.cmi.wangmaoren.client.classicsDesignPattern;

import af.javafx.layout.AfLinearLayout;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

public class ConsistentCompositeClient extends Application
{
	private AfLinearLayout root = new AfLinearLayout();
	private AfLinearLayout line = new AfLinearLayout();
	private Button addCompositeButton = new Button("添加组合键");
	private Button addLeafButton = new Button("添加叶节点");
	private Button deleteButton = new Button("删除");
	private Label topBlank = new Label();
	private Label bottomBlank = new Label();
	private TextField nameText = new TextField();
	private TreeView<ItemData> treeView = new TreeView<ItemData>();

	@Override
	public void start(Stage primaryStage)
	{
		try
		{
			initLayout();// 初始化界面
			initTreeView();

			Scene scene = new Scene(root, 750, 463);
			primaryStage.setTitle("一致型组合模式");
			primaryStage.setScene(scene);
			primaryStage.show();

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		launch(args);
	}
	
	public void openWindow()
	{
		initLayout();//初始化界面
		initTreeView();
		
		Scene scene = new Scene(root,750,463);
		Stage stage = new Stage();
		stage.setTitle("一致型组合模式");
		stage.setScene(scene);
		stage.show();
	}

	/*
	 * 添加组合键
	 */
	private void addComposite()
	{
		String name = nameText.getText();//获取输入框文字
		TreeItem<ItemData> item_selectItem = treeView.getSelectionModel().getSelectedItem();//获取选中的节点

		nameText.clear();//清空输入框文字

		//如果输入框不为空，选中节点不为空，选中节点是目录，则加入新节点
		if (!name.isEmpty() && item_selectItem != null && item_selectItem.getValue().isDir)
		{
			name = "Team团队：" + name;

			ItemData data_compsite = new ItemData(true, name);//创建新节点，设置为目录节点
			TreeItem<ItemData> item_compsite = new TreeItem<ItemData>(data_compsite);
			item_compsite.setExpanded(true);

			item_selectItem.getChildren().add(item_compsite);//加入新节点
		}
	}

	/*
	 * 添加叶节点
	 */
	private void addLeaf()
	{
		String name = nameText.getText();//获取输入框文字
		TreeItem<ItemData> item_selectItem = treeView.getSelectionModel().getSelectedItem();//获取选中的节点

		nameText.clear();//清空输入框文字

		//如果输入框不为空，选中节点不为空，选中节点是目录，则加入新节点
		if (!name.isEmpty() && item_selectItem != null && item_selectItem.getValue().isDir)
		{
			name = "Person个人：" + name;

			ItemData data_leaf = new ItemData(false, name);//创建新节点，设置为非目录节点
			TreeItem<ItemData> item_leaf = new TreeItem<ItemData>(data_leaf);
			item_leaf.setExpanded(true);

			item_selectItem.getChildren().add(item_leaf);//加入新节点
		}
	}

	/*
	 * 删除
	 */
	private void delete()
	{
		TreeItem<ItemData> item_selectItem = treeView.getSelectionModel().getSelectedItem();//获取选中的节点

		if (item_selectItem != null)
		{
			item_selectItem.getParent().getChildren().remove(item_selectItem);//删除选中节点
		}
	}

	/*
	 * 初始化界面
	 */
	private void initLayout()
	{
		//设置布局
		root.orientation(Orientation.HORIZONTAL).spacing(20).padding(20);
		line.orientation(Orientation.VERTICAL).spacing(35);

		line.getChildren().addAll(topBlank, nameText, addCompositeButton, addLeafButton, deleteButton, bottomBlank);
		line.layout("1 60px 60px 60px 60px 1");
		root.add(treeView, "1");
		root.add(line, "250px");

		nameText.setPromptText("请输入组合件/叶节点名字");
		
		//设置字体大小
		addCompositeButton.setFont(Font.font("华文中宋", 20));
		addLeafButton.setFont(Font.font("华文中宋", 20));
		deleteButton.setFont(Font.font("华文中宋", 20));
		nameText.setFont(Font.font("华文中宋", 19));

		//设置图片显示
		Image teamImage = new Image(getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/composite/team.png"));
		addCompositeButton.setGraphic(new ImageView(teamImage));
		Image personImage = new Image(
				getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/composite/person.png"));
		addLeafButton.setGraphic(new ImageView(personImage));
		Image deleteImage = new Image(
				getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/composite/delete.png"));
		deleteButton.setGraphic(new ImageView(deleteImage));

		//设置按钮控件事件处理
		addCompositeButton.setOnAction((e) -> {
			addComposite();
		});
		addLeafButton.setOnAction((e) -> {
			addLeaf();
		});
		deleteButton.setOnAction((e) -> {
			delete();
		});

	}

	/*
	 * 初始化树控件
	 */
	private void initTreeView()
	{
		ItemData data_root = new ItemData(true, "Organization组织");
		TreeItem<ItemData> item_root = new TreeItem<ItemData>(data_root);
		item_root.setExpanded(true);

		treeView.setRoot(item_root);// 设置根节点

		// 设置 CellFactory
		treeView.setCellFactory(new Callback<TreeView<ItemData>, TreeCell<ItemData>>()
		{

			@Override
			public TreeCell<ItemData> call(TreeView<ItemData> param)
			{
				return new MyTreeCell();
			}

		});
	}

	private static class MyTreeCell extends TreeCell<ItemData>
	{

		@Override
		protected void updateItem(ItemData item, boolean empty)
		{
			super.updateItem(item, empty);
			if (item == null)
			{
				this.setText("");
			} else
			{
				this.setText(item.name);
			}
		}
	}

	private static class ItemData
	{
		public String name; // 文件名或目录名
		public boolean isDir = false; // 是否目录

		public ItemData(boolean isDir, String name)
		{
			this.isDir = isDir;
			this.name = name;
		}
	}
}